package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductAddListReq;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductAddListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ShoppingcartProductAddList {
	
	private static ShoppingcartProductAddListCall shoppingcartProductAddListCall = new ShoppingcartProductAddListCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	ShoppingcartProductAddListReq req = new ShoppingcartProductAddListReq();
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加多个商品");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductAddListCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_001() throws Exception {
		Logger.start(true,"购物车添加单个商品");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductAddListReq req = new ShoppingcartProductAddListReq();
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean);
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_002() throws Exception {
		Logger.start(true,"购物车添加多个商品");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "catalogId", "catalogId2"));
			ShoppingcartProductBean shoppingcartProductBean3 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "source", 1));
			ShoppingcartProductBean shoppingcartProductBean4 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "source", 2));
			ShoppingcartProductAddListReq req = new ShoppingcartProductAddListReq();
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			addList.add(shoppingcartProductBean3);
			addList.add(shoppingcartProductBean4);
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(4, ymtShoppingCartWapper.getCountByUserId(userId), "数据库存储条数不正确");
			
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean1);
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean2);
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean3);
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean4);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_003() throws Exception {
		Logger.start(true,"传入错误的sellerid");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "catalogId", "catalogId2", "sellerId", 0));
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int bCode = ret.getInt("bCode");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals(100, bCode, "接口bCode返回异常");
			Logger.verifyEquals("list[1].sellerId:错误的sellerId;", message, "sellerid错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_004() throws Exception {
		Logger.start(true,"传入错误的userId");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", 0, "catalogId", "catalogId2"));
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int bCode = ret.getInt("bCode");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals(100, bCode, "接口bCode返回异常");
			Logger.verifyEquals("list[1].userId:错误的userId;", message, "userid错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_005() throws Exception {
		Logger.start(true,"传入错误的num");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "catalogId", "catalogId2", "num", 0));
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int bCode = ret.getInt("bCode");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals(100, bCode, "接口bCode返回异常");
			Logger.verifyEquals("list[1].num:错误的商品数量;", message, "num错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_006() throws Exception {
		Logger.start(true,"传入错误的catalogId");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "catalogId", ""));
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int bCode = ret.getInt("bCode");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals(100, bCode, "接口bCode返回异常");
			Logger.verifyEquals("list[1].catalogId:规格不能为空;", message, "catalogId错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_007() throws Exception {
		Logger.start(true,"传入错误的productId");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			ShoppingcartProductBean shoppingcartProductBean2 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "productId", ""));
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int bCode = ret.getInt("bCode");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals(100, bCode, "接口bCode返回异常");
			Logger.verifyEquals("list[1].productId:商品id不能为空;", message, "productId错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductAddList_008() throws Exception {
		Logger.start(true,"可添加成功最少参数验证");
		try{
			ShoppingcartProductBean shoppingcartProductBean1 = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", 55421));
			ShoppingcartProductBean shoppingcartProductBean2 = new ShoppingcartProductBean();
			
			shoppingcartProductBean2.setUserId(55421);
			shoppingcartProductBean2.setSellerId(44213);
			shoppingcartProductBean2.setCatalogId("catalogId-min");
			shoppingcartProductBean2.setProductId("productId-min");
			shoppingcartProductBean2.setNum(5);
			
			ArrayList<ShoppingcartProductBean> addList = new ArrayList<ShoppingcartProductBean>();
			
			addList.add(shoppingcartProductBean1);
			addList.add(shoppingcartProductBean2);
			
			req.setList(addList);
			
			JSONObject ret = shoppingcartProductAddListCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.
					getByCatalogIdIdAndUserId(shoppingcartProductBean2.getCatalogId(), 55421);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(shoppingcartProductBean2.getUserId(), Integer.parseInt(shoppingcartProductDB.get("userid").toString()), "userId错误");
			Logger.verifyEquals(shoppingcartProductBean2.getSellerId(), Integer.parseInt(shoppingcartProductDB.get("sellerid").toString()), "sellerId错误");
			Logger.verifyEquals(shoppingcartProductBean2.getCatalogId(), shoppingcartProductDB.get("catalogid").toString(), "catalogId错误");
			Logger.verifyEquals(shoppingcartProductBean2.getProductId(), shoppingcartProductDB.get("productid").toString(), "productId错误");
			Logger.verifyEquals(shoppingcartProductBean2.getNum(), Integer.parseInt(shoppingcartProductDB.get("num").toString()), "num错误");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
